/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tp_imag.gui.area.selection;
import ij.*;
import ij.plugin.filter.PlugInFilter;
import ij.process.*;
import ij.gui.*;

import java.awt.Canvas;
import java.awt.event.*;

/** Mouse_Listener
  *
  * Plugin for listening to mouse clicks in image window.
  *
  * This is an example plugin from the ImageJ plugin writing tutorial.
  * The tutorial can be downloaded at
  * http://www.fh-hagenberg.at/mtd/depot/imaging/imagej
  */

/**
 *
 * @author LRinaudo
 */
public class Mouse_Listener implements PlugInFilter, MouseListener {
	private static double PORCENTAGE = 8;
    ImagePlus img;
    ImagePlus newImage;
    ImageCanvas canvas;
    javax.swing.JPanel jPanel;
    int xInitial;
    int yInitial;

    public int setup(String arg, ImagePlus img) {
        this.img = img;
        return DOES_ALL;
    }

    public int setup(String arg, ImagePlus img, javax.swing.JPanel jPanel) {
        this.img = img;
        this.jPanel = jPanel;
        return DOES_ALL+NO_CHANGES+ROI_REQUIRED;
    }

    public void run(ImageProcessor ip) {
    	this.canvas = new ImageCanvas(this.img);
    	this.canvas.addMouseListener(this);
    	this.jPanel.addMouseListener(this);
    }

	// method is called when mouse is clicked
    public void mouseClicked(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        int offscreenX = canvas.offScreenX(x);
        int offscreenY = canvas.offScreenY(y);
        IJ.showMessage("mousePressed: "+offscreenX+","+offscreenY);
    }

    public void mousePressed(MouseEvent e) {
    	this.xInitial = e.getX();
    	this.yInitial = e.getY();       
    }

    public void mouseReleased(MouseEvent e) {
    	int xFinal = e.getX();
    	int yFinal = e.getY();
    	
    	this.newImage = new ImagePlus();
    	this.img.setRoi((int)(this.xInitial* PORCENTAGE) ,(int)( this.yInitial * PORCENTAGE), (int)((xFinal - this.xInitial)* PORCENTAGE), (int) ((yFinal - this.yInitial) * PORCENTAGE));
    	//this.img.draw(this.xInitial, this.yInitial, xFinal - this.xInitial, yFinal - this.yInitial);
    	this.img.show();
    	this.img.updateAndDraw();
    	this.img.copy(false);
    	this.newImage =ImagePlus.getClipboard();
    	//this.createNewImage(this.img.getProcessor());
    	
    }

    /*public void createNewImage(ImageProcessor bgIp) {
    	 // bgIp = background image
    	
    	 int w = bgIp.getWidth();
    	 int h = bgIp.getHeight();
    	
    	 // prepare foreground image
    	 ImageProcessor fgIp = fgIm.getProcessor().convertToByte(false);
    	 ImageProcessor fgTmpIp = bgIp.duplicate();
    	
    	 // create image stack
    	 ImagePlus movie = NewImage.createByteImage("Movie",w,h,nFrames,0);
    	 ImageStack stack = movie.getStack();
    	
    	 // loop over stack frames
    	 for (int i=0; i<nFrames; i++) {
    	 // transparency of foreground image
    	 double iAlpha = 1.0 - (double)i/(nFrames-1);
    	 ImageProcessor iFrame = stack.getProcessor(i+1);
    	
    	// copy background image to frame i
    	 iFrame.insert(bgIp,0,0);
    	 iFrame.multiply(iAlpha);
    	
    	 // copy foreground image and make transparent
    	 fgTmpIp.insert(fgIp,0,0);
    	 fgTmpIp.multiply(1-iAlpha);
    	
    	 // add foreground image frame i
    	 ByteBlitter blitter =
    	 new ByteBlitter((ByteProcessor)iFrame);
    	 blitter.copyBits(fgTmpIp,0,0,Blitter.ADD);
    	 }
    	 // display movie (image stack)
    	 movie.show();
    }*/
    	 
    
    public void mouseEntered(MouseEvent e) {}

    public void mouseExited(MouseEvent e) {}

    public ImagePlus getImage(){
    	return this.newImage;
    }
}
